package rafael.bot;

import static rafael.bot.VectorUtils.*;

class PerceptronMulticlass {
	private double[][] w;
	public PerceptronMulticlass(int classes) {
		w = new double[classes][];
	}
	public void init(int inputLength) {
		for(int i = 0; i < w.length; i++) {
			w[i] = new double[inputLength];
		}
	}
	public void onlineTrain(double[] x, double yReal, double eta) {
		int y_cap = (int)predict(x);
		int y = (int)yReal;
		if(y_cap != y) {
			w[y_cap] = minus(w[y_cap], scalar(eta,x));
			w[y] = plus(w[y],scalar(eta,x));
		}
	}
	public double predict(double[] x) {
		double max = Double.NEGATIVE_INFINITY;
		double wx;
		int best = 0;
		for(int i = 0; i < w.length; i++) {
			wx = dot(w[i],x);
			if(wx > max) {
				max = wx;
				best = i;
			}
		}
		return best;
	}
}
